package com.sky.task;

import com.sky.dto.common.QueryParams;
import com.sky.entity.CompetionCategory;
import com.sky.mapper.CompetionCategoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class CategoryTask {
    @Autowired
    private CompetionCategoryMapper competionCategoryMapper;
    @Scheduled(cron = "0 * * * * ?")  // 每分钟执行一次
    public void processTimeoutOrder(){
        QueryParams queryParams = new QueryParams();
        queryParams.setExtra(3); //排除已完成的任务
        List<CompetionCategory> results = competionCategoryMapper.getAllList(queryParams);
        log.info("定时任务更新竞赛分类状态: {}", results);
        if(results != null && results.size() > 0){
            results.forEach(item -> {
                // 获取当前的时间 就行判断
                LocalDateTime now = LocalDateTime.now();
                LocalDateTime startTime = item.getStartTime();
                LocalDateTime endTime = item.getEndTime();
                if (endTime != null && now.isAfter(endTime)) {
                    item.setStatus(3); // 3 表示已结束
                }
                else if (startTime != null && endTime != null
                        && (now.isEqual(startTime) || now.isAfter(startTime))
                        && (now.isEqual(endTime) || now.isBefore(endTime))) {
                    item.setStatus(2); // 2 表示进行中
                }
                else if (startTime != null && now.isBefore(startTime)) {
                    item.setStatus(1); // 1 表示未开始
                }
                log.info("定时任务更新竞赛分类状态: {}", item);
                competionCategoryMapper.update(item);
            });
        }
    }

//    @Scheduled(cron = "0 0 1 * * ?")  // 每天凌晨1点执行

}
